package nullInvader.View;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

/**
 * This class constitutes a game map and is a custom JComponent. An
 * object of this class is used by the GUI to display what's actually
 * happening in the game. It also allows for fast custom drawing of
 * game objects.
 * 
 * @author Emanuel Yifter Lindgren ( dv11elr )
 * @version 1.0
 */
public class GameMapComponent extends JComponent {

    private static final long serialVersionUID = 1L;
    private BufferedImage image;

    /**
     * Constructs a new GameMapComponent with a specified width and
     * height.
     * 
     * @param width
     *            The desired width of the component
     * @param height
     *            The desired height of the component
     */
    public GameMapComponent(int width, int height) {
        super();
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        /*
         * Draw the component black to prevent system dependent
         * default color.
         */
        Graphics g = image.createGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, width, height);
    }

    /**
     * Bypass JComponents method to paint this component. Draw our
     * BufferedImage instead.
     * 
     * @param g
     *            the graphics context.
     * @see javax.swing.JComponent#paintComponent
     */
    @Override
    public synchronized void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    /**
     * Submits the next BufferedImage to be drawn on this component in
     * the next call to paintComponent.
     * 
     * @param image
     *            the image to draw on this component.
     * @param x
     *            the x-axis coordinate to start drawing from.
     * @param y
     *            the y-axis coordinate to start drawing from.
     */
    public synchronized void drawImage(BufferedImage image, int x, int y) {
        this.image = image;

    }
}
